{***************************************************************
    SpyGestion  Copyright (C) 2008 Christophe KOALI <nature2gas@gmail.com>
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
****************************************************************}
unit modSimpleRemplace;

interface

uses Classes, SysUtils, sFonctionsChaines;

type
    TSimpleRemplaceStr = class(TStringList)
    private
       FNouvellesChaines: TStringList;

    public
       function LoadFromFile(Fichier: string): integer;

       procedure Applique(var Str: string);

       procedure Delete(Index: Integer); override;
       procedure Clear; override;

       constructor Create;
       destructor Destroy; override;

    end;

    
implementation

procedure TSimpleRemplaceStr.Delete(Index: Integer);
begin
    FNouvellesChaines.Delete(Index);
    inherited Delete(Index);
end;

procedure TSimpleRemplaceStr.Clear;
begin
    FNouvellesChaines.Clear;
    inherited Clear;
end;

procedure TSimpleRemplaceStr.Applique(var Str: string);
var i: integer;
begin

    if Count = 0 then exit;

    for i := 0 to Count - 1 do begin
        Str := sfcRemplace(Str, Strings[i], FNouvellesChaines.Strings[i]);

    end;

end;

function TSimpleRemplaceStr.LoadFromFile(Fichier: string): integer;
type TBlockReading = (brAucun, brStart, brReplace);
var F: TextFile;
    Ligne, startBuffer, replaceBuffer: string;
    InStrBlock: TBlockReading;
begin

    Result := 0;

    if FileExists(Fichier) = false then exit;


    AssignFile(F, Fichier);
    
    
    {$I-}
    ReSet(F);
    {$I+}


    // IOResult renvoie 0 => cela signifie que la dernière opération d'E/S a réussi
    if NoT(IOResult = 0) then begin
       Result := -1;
       exit;
    end;

    InStrBlock := brAucun;
    startBuffer   := '';
    replaceBuffer := '';

    While NoT(EoF(F)) do begin
         // Lit une ligne
         ReadLn(F, Ligne);

         case InStrBlock of
              brAucun: begin
                          Ligne := trim(Ligne);
                          if Ligne = '' then Continue;
                          if Ligne[1] <> '[' then Continue;
                          //if Ligne[1] = ';' then Continue;
                          //if Ligne[1] = '#' then Continue;
                          //if Ligne[1] = '/' then Continue;
                          //if Ligne[1] = '@' then Continue;

                          startBuffer   := '';
                          replaceBuffer := '';

                          if SameText(Ligne, '[start]') then begin
                             InStrBlock := brStart;
                          end;

                          // si c'est un autre block on ignore


                       end;
              brStart: begin
                          if SameText(Ligne, '[replace]') then begin
                             // on entre dans le block replace
                             InStrBlock := brReplace;
                             Continue;
                          end;

                          startBuffer := startBuffer + Ligne;
                       end;

              brReplace: begin
                          if SameText(Ligne, '[end]') then begin
                             // on a terminé
                             // la chaine de remplacement peut être vide
                             if (startBuffer <> '')  then begin
                                Add(startBuffer);
                                FNouvellesChaines.Add(replaceBuffer);
                             end;

                             startBuffer   := '';
                             replaceBuffer := '';

                             InStrBlock := brAucun;
                             Continue;
                          end;

                          replaceBuffer := replaceBuffer + Ligne;
                       end;
         end;
         
    end;
    
    // Fermeture du fichier
    CloseFile(F);

end;


constructor TSimpleRemplaceStr.Create;
begin
    inherited Create;

    FNouvellesChaines := TStringList.Create;

end;

destructor TSimpleRemplaceStr.Destroy;
begin
    FNouvellesChaines.Free;

    inherited Destroy;
end;

end.
